home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / readwrite.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  363 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <stdlib.h>
  43. #include <X11/keysym.h>
  44.  
  45. static int RGB_SB_attributes[] = {
  46.     GLX_RGBA,
  47.     GLX_RED_SIZE, 1,
  48.     GLX_GREEN_SIZE, 1,
  49.     GLX_BLUE_SIZE, 1,
  50.     GLX_DEPTH_SIZE, 1,
  51.     GLX_STENCIL_SIZE, 1,
  52.     None,
  53. };
  54.  
  55. static int CI_SB_attributes[] = {
  56.     GLX_DEPTH_SIZE, 1,
  57.     GLX_STENCIL_SIZE, 1,
  58.     None,
  59. };
  60.  
  61. static int RGB_DB_attributes[] = {
  62.     GLX_RGBA,
  63.     GLX_RED_SIZE, 1,
  64.     GLX_GREEN_SIZE, 1,
  65.     GLX_BLUE_SIZE, 1,
  66.     GLX_DOUBLEBUFFER,
  67.     GLX_DEPTH_SIZE, 1,
  68.     GLX_STENCIL_SIZE, 1,
  69.     None,
  70. };
  71.  
  72. static int CI_DB_attributes[] = {
  73.     GLX_DOUBLEBUFFER,
  74.     GLX_DEPTH_SIZE, 1,
  75.     GLX_STENCIL_SIZE, 1,
  76.     None,
  77. };
  78.  
  79.  
  80. Display *dpy;
  81. Window window;
  82. int rgb = 1;
  83. GLenum direct = GL_TRUE;
  84. int W = 600;
  85. int H = 600;
  86. int doubleBuf = 1;
  87. int dither = 1;
  88. char rawData[1280*1024*4];    /* big enough for a cheap demo */
  89.  
  90. static void Init(void)
  91. {
  92.     if (rgb) {
  93.     glClearColor(0,0,0,0);
  94.     } else {
  95.     glClearIndex(0);
  96.     }
  97.  
  98.     glClear(GL_COLOR_BUFFER_BIT);
  99.     glReadBuffer(GL_FRONT);
  100.     glDrawBuffer(GL_FRONT);
  101. }
  102.  
  103. static void Redraw(void)
  104. {
  105.     int i;
  106.     int r,g;
  107.  
  108.     glViewport(0,0,W,H);
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     glOrtho(0,W,0,H,-1,1);
  112.     glMatrixMode(GL_MODELVIEW);
  113.  
  114.     glClear(GL_COLOR_BUFFER_BIT);
  115.  
  116.     r = g = 0;
  117.  
  118.     glIndexf(0);
  119.     glBegin(GL_QUAD_STRIP);
  120.     glColor3ub(r,g,0);
  121.     glVertex2f(0, 0);
  122.     glColor3ub(r,g,255);
  123.     glVertex2f(0, H);
  124.  
  125.     r = 255;
  126.  
  127.     glIndexf(85);
  128.     glColor3ub(r,g,0);
  129.     glVertex2f(W/3, 0);
  130.     glColor3ub(r,g,255);
  131.     glVertex2f(W/3, H);
  132.  
  133.     g = 255;
  134.  
  135.     glIndexf(170);
  136.     glColor3ub(r,g,0);
  137.     glVertex2f(W*2/3, 0);
  138.     glColor3ub(r,g,255);
  139.     glVertex2f(W*2/3, H);
  140.  
  141.     r = 0;
  142.  
  143.     glIndexf(255);
  144.     glColor3ub(r,g,0);
  145.     glVertex2f(W, 0);
  146.     glColor3ub(r,g,255);
  147.     glVertex2f(W, H);
  148.  
  149.     glEnd();
  150. }
  151.  
  152. static void pixelsTest(void)
  153. {
  154.     if (rgb) {
  155.     glReadPixels(0,0,W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
  156.     glRasterPos2f(0,0);
  157.     glDrawPixels(W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
  158.     } else {
  159.     glReadPixels(0,0,W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
  160.     glRasterPos2f(0,0);
  161.     glDrawPixels(W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
  162.     }
  163. }
  164.  
  165. static void blendTest(void)
  166. {
  167.     if (!rgb) {
  168.     printf("blend Test not supported in color index mode.\n");
  169.     return;
  170.     }
  171.     glEnable(GL_BLEND);
  172.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  173.     glColor4f(1.0, 1.0, 1.0, 0.0);
  174.     glBegin(GL_QUADS);
  175.     glVertex2f(0,0);
  176.     glVertex2f(W,0);
  177.     glVertex2f(W,H);
  178.     glVertex2f(0,H);
  179.     glEnd();
  180.     glDisable(GL_BLEND);
  181.     glFlush();
  182. }
  183.  
  184. static void Usage(void)
  185. {
  186.     fprintf(stderr, "Usage: program [-c] [-s] [-i]\n");
  187.     fprintf(stderr, " -c  run in color index mode\n");
  188.     fprintf(stderr, " -s  run in singlebuffer mode\n");
  189.     fprintf(stderr, " -i  turn direct rendering off--go thru the X server\n");
  190.     exit(1);
  191. }
  192.  
  193. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  194. {
  195.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  196.     return GL_TRUE;
  197.     }
  198.     return GL_FALSE;
  199. }
  200.  
  201. int main(int argc, char** argv)
  202. {
  203.     XVisualInfo *vi;
  204.     Colormap cmap;
  205.     XSetWindowAttributes swa;
  206.     GLXContext cx;
  207.     XEvent event;
  208.     GLboolean needDisplay;
  209.     XColor white;
  210.     int i;
  211.  
  212.     rgb = 1;
  213.     direct = GL_TRUE;
  214.     for (i = 1; i < argc; i++) {
  215.         if (argv[i][0] == '-') {
  216.             switch (argv[i][1]) {
  217.               case 'c':
  218.                 rgb = GL_FALSE;
  219.                 break;
  220.           case 's':
  221.         doubleBuf = 0;
  222.         break;
  223.           case 'i':
  224.         direct = GL_FALSE;
  225.         break;
  226.               default:
  227.                 Usage();
  228.             }
  229.         } else {
  230.             Usage();
  231.         }
  232.     }
  233.  
  234.     dpy = XOpenDisplay(0);
  235.     if (!dpy) {
  236.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  237.     return -1;
  238.     }
  239.  
  240.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  241.         doubleBuf ? (rgb ? RGB_DB_attributes : CI_DB_attributes) :
  242.         (rgb ? RGB_SB_attributes : CI_SB_attributes));
  243.     if (!vi) {
  244.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  245.         getenv("DISPLAY"));
  246.     return -1;
  247.     }
  248.  
  249.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  250.                rgb ? AllocNone : AllocAll);
  251.     if (rgb) {
  252.     white.red = ~0;
  253.     white.green = ~0;
  254.     white.blue = ~0;
  255.     XAllocColor(dpy, cmap, &white);
  256.     swa.background_pixel = white.pixel;
  257.     } else {
  258.     swa.background_pixel = 15;
  259.     }
  260.  
  261.     if (!rgb) {
  262.     XColor buf;
  263.     int i;
  264.  
  265.     buf.flags = DoRed | DoGreen | DoBlue;
  266.  
  267.     /* Init color map */
  268.     for (i=0; i<16; i++) {
  269.         buf.pixel = i;
  270.         buf.blue = (i & 4) ? 65535 : 0;
  271.         buf.green = (i & 2) ? 65535 : 0;
  272.         buf.red = (i & 1) ? 65535 : 0;
  273.         if (i > 8) {
  274.         buf.red /= 2;
  275.         buf.green /= 2;
  276.         buf.blue /= 2;
  277.         }
  278.         XStoreColor(dpy, cmap, &buf);
  279.     }
  280.     }
  281.  
  282.     swa.border_pixel = 0;
  283.     swa.background_pixel = white.pixel;
  284.     swa.colormap = cmap;
  285.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  286.     | KeyReleaseMask;
  287.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  288.                W, H,
  289.                0, vi->depth, InputOutput, vi->visual,
  290.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  291.                &swa);
  292.     XSetWMColormapWindows(dpy, window, &window, 1);
  293.     XMapWindow(dpy, window);
  294.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  295.  
  296.     cx = glXCreateContext(dpy, vi, 0, direct);
  297.     if (!glXMakeCurrent(dpy, window, cx)) {
  298.     fprintf(stderr, "Can't make window current to context\n");
  299.     return -1;
  300.     }
  301.  
  302.     Init();
  303.  
  304.     needDisplay = GL_TRUE;
  305.     for (;;) {
  306.     do {
  307.         XNextEvent(dpy, &event);
  308.         switch (event.type) {
  309.           case Expose:
  310.         needDisplay = GL_TRUE;
  311.         break;
  312.           case ConfigureNotify:
  313.         W = event.xconfigure.width;
  314.         H = event.xconfigure.height;
  315.         needDisplay = GL_TRUE;
  316.         break;
  317.           case KeyPress:
  318.         {
  319.             char buf[100];
  320.             int rv;
  321.             KeySym ks;
  322.  
  323.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  324.             switch (ks) {
  325.               case XK_P: case XK_p:
  326.             printf("pixels test (ReadPixels followed by DrawPixels)\n");
  327.             pixelsTest();
  328.             break;
  329.               case XK_B: case XK_b:
  330.             printf("blend test\n");
  331.             blendTest();
  332.             break;
  333.               case XK_R: case XK_r:
  334.             needDisplay = GL_TRUE;
  335.             break;
  336.               case XK_D: case XK_d:
  337.             dither = 1-dither;
  338.             if (dither) {
  339.                 glEnable(GL_DITHER);
  340.                 printf("Dithering on\n");
  341.             } else {
  342.                 glDisable(GL_DITHER);
  343.                 printf("Dithering off\n");
  344.             }
  345.             needDisplay = GL_TRUE;
  346.             break;
  347.               case XK_Escape:
  348.             return 0;
  349.               default:
  350.             break;
  351.             }
  352.         }
  353.         break;
  354.         }
  355.     } while (XPending(dpy) != 0);
  356.  
  357.     if (needDisplay) {
  358.         needDisplay = GL_FALSE;
  359.         Redraw();
  360.     }
  361.     }
  362. }
  363.